Commiting patch r15625:15661 from branches/SerbianVariants.
authorRobert Stojnić <rainman@users.mediawiki.org>
Tue, 18 Jul 2006 19:36:25 +0000 (19:36 +0000)
committerRobert Stojnić <rainman@users.mediawiki.org>
Tue, 18 Jul 2006 19:36:25 +0000 (19:36 +0000)
Enable titles in fixed variants (e.g. -{Title}-), rewrote
the Parser code for variants (do a single query for all
variants of all links, instead 1 query per link per variant).

includes/Parser.php
languages/Language.php
languages/LanguageConverter.php

index 1079f5b..74b926f 100644 (file)
@@ -1408,7 +1408,6 @@ class Parser
                $selflink = $this->mTitle->getPrefixedText();
                wfProfileOut( $fname.'-setup' );
 
-               $checkVariantLink = sizeof($wgContLang->getVariants())>1;
                $useSubpages = $this->areSubpagesAllowed();
 
                # Loop for each link
@@ -1492,13 +1491,6 @@ class Parser
                                continue;
                        }
 
-                       #check other language variants of the link
-                       #if the article does not exist
-                       if( $checkVariantLink
-                           && $nt->getArticleID() == 0 ) {
-                               $wgContLang->findVariantLink($link, $nt);
-                       }
-
                        $ns = $nt->getNamespace();
                        $iw = $nt->getInterWiki();
 
@@ -3830,6 +3822,7 @@ class Parser
        function replaceLinkHolders( &$text, $options = 0 ) {
                global $wgUser;
                global $wgOutputReplace;
+               global $wgContLang, $wgLanguageCode;
 
                $fname = 'Parser::replaceLinkHolders';
                wfProfileIn( $fname );
@@ -3920,6 +3913,86 @@ class Parser
                        }
                        wfProfileOut( $fname.'-check' );
 
+                       # Do a second query for links in different language variants (if needed)
+                       if(sizeof($wgContLang->getVariants())>1){
+                               $linkBatch = new LinkBatch(); 
+
+                               // Add variants of links to link batch
+                               foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
+                                       $title = $this->mLinkHolders['titles'][$key];
+                                       if ( is_null( $title ) )
+                                               continue;
+
+                                       $pdbk = $title->getPrefixedDBkey();
+
+                                       // generate all variants of the link title text
+                                       $allTextVariants = $wgContLang->convertLinkToAllVariants($title->getText());
+
+                                       // if link was not found (in first query), add all variants to query
+                                       if ( !isset($colours[$pdbk]) ){
+                                               foreach($allTextVariants as $textVariant){
+                                                       $linkBatch->addObj( Title::makeTitleSafe( $ns, $textVariant ) );
+                                               }
+                                       }
+                                       // if link was found add only variant with fixed title
+                                       else if($colours[$pdbk] == 1){
+                                               $fixedCode = $wgLanguageCode.'-fixed';
+                                               if( isset($allTextVariants[$fixedCode]) ){
+                                                       $linkBatch->addObj( Title::makeTitleSafe( $ns, $allTextVariants[$fixedCode]  ) );
+                                               }
+                                       }
+                               }
+
+                               # fetch link variants into cache
+                               $linkBatch->execute();
+
+                               # check if links are found in some of the variants
+                               foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
+                                       $title = $this->mLinkHolders['titles'][$key];
+                                       if ( is_null( $title ) ) 
+                                               continue;
+
+                                       $pdbk = $title->getPrefixedDBkey();                                     
+                                       $allTextVariants = $wgContLang->convertLinkToAllVariants($title->getText());
+
+                                       // If link has already been found, check only fixed variant
+                                       if(isset($colours[$pdbk]) && $colours[$pdbk] == 1){
+                                               $fixedCode = $wgLanguageCode.'-fixed';
+
+                                               if( isset($allTextVariants[$fixedCode]) ){
+                                                       $allTextVariants = array($fixedCode => $allTextVariants[$fixedCode]);
+                                               }
+                                       }
+
+                                       // process the link variants
+                                       if ( !isset($colours[$pdbk]) || $colours[$pdbk] == 1 ){
+                                               foreach($allTextVariants as $variantCode => $textVariant){
+                                                       $variantTitle=Title::makeTitleSafe( $ns, $textVariant );
+                                                       if(is_null($variantTitle)) continue;
+
+                                                       $varpdbk = $variantTitle->getPrefixedDBkey();
+
+                                                       if($linkCache->getGoodLinkID( $varpdbk ) != 0){
+
+                                                               // found link in some of the variants, replace the link holder data
+                                                               $this->mLinkHolders['titles'][$key] = $variantTitle;
+                                                               $this->mLinkHolders['dbkeys'][$key] = $variantTitle->getDBkey();
+
+                                                               if($wgContLang->getPreferredVariant() == $wgLanguageCode)
+                                                                       $this->mLinkHolders['texts'][$key] = $this->mLinkHolders['texts'][$key];
+                                                               else
+                                                                       $this->mLinkHolders['texts'][$key] = $variantTitle->getText();
+
+                                                               $pdbks[$key] = $varpdbk;
+                                                               $colours[$varpdbk] = 1;
+
+                                                               break;
+                                                       }
+                                               }       
+                                       }
+                               }
+                       }
+
                        # Construct search and replace arrays
                        wfProfileIn( $fname.'-construct' );
                        $wgOutputReplace = array();
index ba4e713..650d5c3 100644 (file)
@@ -315,6 +315,7 @@ class fakeConverter {
        function getParsedTitle() {return '';}
        function markNoConversion($text) {return $text;}
        function convertCategoryKey( $key ) {return $key; }
+       function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
 
 }
 
@@ -1153,6 +1154,17 @@ class Language {
                $this->mConverter->findVariantLink($link, $nt);
        }
 
+       /**
+        * If a language supports multiple variants, converts text
+        * into an array of all possible variants of the text:
+        *  'variant' => text in that variant
+        */
+
+       function convertLinkToAllVariants($text){
+               return $this->mConverter->convertLinkToAllVariants($text);
+       }
+
+
        /**
         * returns language specific options used by User::getPageRenderHash()
         * for example, the preferred language variant
index c194ace..575df85 100644 (file)
@@ -36,6 +36,7 @@ class LanguageConverter {
                                                                $markup=array(),
                                                                $flags = array()) {
                global $wgDBname;
+               global $wgLegalTitleChars;
                $this->mLangObj = $langobj;
                $this->mMainLanguageCode = $maincode;
                $this->mVariants = $variants;
@@ -46,6 +47,11 @@ class LanguageConverter {
                $this->mMarkup = array_merge($m, $markup);
                $f = array('A'=>'A', 'T'=>'T');
                $this->mFlags = array_merge($f, $flags);
+
+               // enable escape characters -{ }- in titles
+               if(!preg_match('/\{/',$wgLegalTitleChars)) $wgLegalTitleChars.='\{';
+               if(!preg_match('/\}/',$wgLegalTitleChars)) $wgLegalTitleChars.='\}';
+
        }
 
        /**
@@ -171,9 +177,9 @@ class LanguageConverter {
      *
      * @param string $text the text to be converted
      * @return array of string
-     * @private
+     * @public
      */
-       function autoConvertToAllVariants($text) {
+       function autoConvertToAllVariants($text, $includeFixedVariant=true) {
                $fname="LanguageConverter::autoConvertToAllVariants";
                wfProfileIn( $fname );
                if( !$this->mTablesLoaded )
@@ -183,10 +189,49 @@ class LanguageConverter {
                foreach($this->mVariants as $variant) {
                        $ret[$variant] = strtr($text, $this->mTables[$variant]);
                }
+               if($includeFixedVariant)
+                       $ret[$this->mMainLanguageCode.'-fixed'] = $this->mMarkup['begin'].$text.$this->mMarkup['end'];
+
                wfProfileOut( $fname );
                return $ret;
        }
 
+       /**
+     * convert link text to all supported variants
+     *
+     * @param string $text the text to be converted
+     * @return array of string
+     * @public
+     */
+       function convertLinkToAllVariants($text,$includeFixedVariant=true) {
+               if( !$this->mTablesLoaded )
+                       $this->loadTables();
+
+               $ret = array();
+               $tarray = explode($this->mMarkup['begin'], $text);
+               $tfirst = array_shift($tarray);
+
+               foreach($this->mVariants as $variant)
+                       $ret[$variant] = strtr($tfirst, $this->mTables[$variant]);
+
+               foreach($tarray as $txt) {
+                       $marked = explode($this->mMarkup['end'], $txt, 2);
+
+                       foreach($this->mVariants as $variant){
+                               $ret[$variant] .= $this->mMarkup['begin'].$marked[0].$this->mMarkup['end'];
+                               if(array_key_exists(1, $marked))
+                                       $ret[$variant] .= strtr($marked[1], $this->mTables[$variant]);
+                       }
+                       
+               }
+
+               if($includeFixedVariant)
+                       $ret[$this->mMainLanguageCode.'-fixed'] = $this->mMarkup['begin'].$text.$this->mMarkup['end'];
+
+               return $ret;
+       }
+
+
        /**
         * Convert text using a parser object for context
         */
@@ -194,7 +239,7 @@ class LanguageConverter {
                global $wgDisableLangConversion;
                /* don't do anything if this is the conversion table */
                if ( $parser->mTitle->getNamespace() == NS_MEDIAWIKI &&
-                       strpos($parser->mTitle->getText, "Conversiontable") !== false ) 
+                                strpos($parser->mTitle->getText(), "Conversiontable") !== false ) 
                {
                        return $text;
                }
@@ -252,7 +297,7 @@ class LanguageConverter {
                                return $text;
                        }
                        else {
-                               $this->mTitleDisplay = $this->autoConvert($text);
+                               $this->mTitleDisplay = $this->convert($text);
                                return $this->mTitleDisplay;
                        }
                }
@@ -289,7 +334,7 @@ class LanguageConverter {
                        else
                                $rules = $marked[0];
 
-#FIXME: may cause trouble here...
+                       //FIXME: may cause trouble here...
                        //strip &nbsp; since it interferes with the parsing, plus,
                        //all spaces should be stripped in this tag anyway.
                        $rules = str_replace('&nbsp;', '', $rules);
@@ -381,23 +426,16 @@ class LanguageConverter {
      * @access public
         */
        function findVariantLink( &$link, &$nt ) {
-               static $count=0; //used to limit this operation
-               static $cache=array();
                global $wgDisableLangConversion;
                $pref = $this->getPreferredVariant();
                $ns=0;
                if(is_object($nt))
                        $ns = $nt->getNamespace();
-               if( $count > 50 && $ns != NS_CATEGORY )
-                       return;
-               $count++;
+
                $variants = $this->autoConvertToAllVariants($link);
                if($variants == false) //give up
                        return;
                foreach( $variants as $v ) {
-                       if(isset($cache[$v]))
-                               continue;
-                       $cache[$v] = 1;
                        $varnt = Title::newFromText( $v, $ns );
                        if( $varnt && $varnt->getArticleID() > 0 ) {
                                $nt = $varnt;